
#pragma once

#include "Panel.h"
#include "ScrollBar.h"
#include "../TextRender.h"
#include "../Preset/ExposedVariable.h"

class TextEditor : public Panel
{
	public:

		TextEditor( Widget* pxParent );
		virtual ~TextEditor();
	
		virtual void		Render			( );
		virtual void		Update			( );

		virtual bool		CanHaveChildren	( ) { return false; }

		virtual void		SetReadOnly( bool bReadOnly );
		virtual void		SetSingleLine( bool bSingleLine );
		virtual void		SetLengthLimit( u_int uLengthLimit );
		virtual void		SetNumberOnly( bool bNumberOnly );
		virtual void		SetShowScrollBars( bool bShowScrollBarV, bool bShowScrollBarH );

		virtual void		Clear();

        virtual void		SetText( const char* szText );
		virtual const char* GetText() { return m_szText; }
		virtual void		AppendText( const char* szText );
		virtual void		SetTextColour( const unsigned int uColour ) { m_uTextColour = uColour; }
		virtual void		SetTextLineColourCallback( LineColourCallback pfnCallback ) { m_pfnLineColourCallback = pfnCallback; }
	
	protected:

		void Caret_Up();
		void Caret_Down();
		void Caret_Left();
		void Caret_Right();
		void Home();
		void End();
		
		void CheckScrolling();
		
		void InsertText( const char* szText );
        void RemoveChar( bool bBeforeCaret, bool bInSelection = false );
		void CopyText( const char* szText );

		bool CanGetFocus() { return !m_bReadOnly; }
		bool RemoveSelectedChars();
		void OnGetFocus();
		void OnLoseFocus();

		char* m_szText;
		char* m_szHighlightedText;
		u_int m_uTextLength;
		u_int m_uTextCapacity;
		u_int m_uTextColour;
		u_int m_uCaretPos;
		u_int m_uHighlightStartPos;
		u_int m_uHighlightEndPos;
		LineColourCallback m_pfnLineColourCallback;
			
		SDL_Rect m_xSDLRect;

		bool m_bReadOnly;
		bool m_bNumberOnly;
		bool m_bSingleLine;
		bool m_bEnforceCapacityLimit;
		bool m_bMouseDownState;
		bool m_bMouseFirstClick;
		bool m_bRecoverFromDoubleClick;
		bool m_bShowingScrollbarV;
		bool m_bShowingScrollbarH;

		static bool s_bCursorBlinkState;
		static float s_fCursorBlinkTimer;
		static float s_fKeyRepeatTimer;
		
		ScrollBar*	m_pxScrollBarV;
		ScrollBar*	m_pxScrollBarH;
};

//eof
